package com.fit.dao.sport;

import java.util.Date;
import java.util.List;

import org.apache.tapestry5.annotations.Log;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hibernate.Session;
import org.hibernate.criterion.Restrictions;

import com.fit.entities.sport.Sport;

public class SportDAOImpl implements SportDAO{

	@Inject
	private Session session;

	@Log
	public boolean save(Sport newSport) {
		if (newSport != null) {
			session.save(newSport);
			return true;
		}
		return false;
	}

	@Log
	public boolean delete(Sport sport) {
		if (sport != null) {
			session.delete(sport);
			return true;
		}
		return false;
	}

	@Log
	public boolean update(Sport sport) {
		if (sport != null) {
			session.update(sport);
			return true;
		}
		return false;
	}

	@Log
	public List findAll() {
		return session.createCriteria(Sport.class).list();
	}

	@Log
	public boolean deleteByID(Long id) {
		Sport u = findByID(id);
		if (u != null) {
			delete(u);
			return true;
		}
		return false;
	}

	@Log
	public Sport findByID(Long id) {
		if (id != null) {
			return (Sport) session.get(Sport.class, id);
		}
		return null;
	}

	@Log
	public Sport findByStardDate(Date startDate) {
		if (startDate == null) {
			return null;
		}

		

		List l = session.createCriteria(Sport.class)
				.add(Restrictions.eq("start_date", startDate)).list();
				
		Sport u = null;
		if (l != null) {
			if (l.size() > 0) {
				u = (Sport) l.get(0);
			}
		}
		return u;
	}



	

	public Sport findByType(String type) {
		if (type == null) {
			return null;
		}

		

		List l = session.createCriteria(Sport.class)
				.add(Restrictions.eq("type", type)).list();
				
		Sport u = null;
		if (l != null) {
			if (l.size() > 0) {
				u = (Sport) l.get(0);
			}
		}
		return u;
	}
	}
